package com.nvc.homeplan;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nvc.homeplan.data.vo.*;


public class PlanListAdapter  extends BaseAdapter 
{
    private static final String CLASSTAG = PlanListAdapter.class.getSimpleName();
    private final Context context;
    private final List<PlanVO> plans;
    private LayoutInflater layoutInflater;
    private final ImageDownloader imageDownloader = new ImageDownloader();

    public PlanListAdapter(Context context, List<PlanVO> plans) {
        this.context = context;
        this.plans = plans;
        Log.v(Constants.LOGTAG, " " + PlanListAdapter.CLASSTAG + " reviews size - " + this.plans.size());
        layoutInflater = (LayoutInflater) this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return this.plans.size();
    }

    public Object getItem(int position) {
        return this.plans.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        PlanVO plan = this.plans.get(position);

        
    	ViewHolder viewHolder = null;
    	
    	TextView title = null;
    	TextView detail = null;
    	TextView orderCode = null;
    	ImageView img=null;
        TextView bedbathLabel = null;
        TextView dimensionLabel = null;
    	
    	if (null == convertView)
    	{
            convertView = layoutInflater.inflate(R.layout.plan_list_single_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
    	}
    	viewHolder = (ViewHolder) convertView.getTag();
    	
    	
    	title = viewHolder.gettitle();
    	title.setText(plan.plantypeName + " : " + plan.id);
		
    	if (!(plan.type == "PP"))
    	{
    		detail = viewHolder.getdetail();
    		detail.setText(plan.finishedsf + " Finished Square Feet");

        	orderCode = viewHolder.getOrderCode();
        	orderCode.setText("Order Code: 00WEB");
        	
    		bedbathLabel = viewHolder.getBedBathLabel();
    		bedbathLabel.setText("Beds: " + plan.noOfBed + " - Baths: " + plan.noOfBath);
    	
    		dimensionLabel = viewHolder.getDimensionLabel();
    		dimensionLabel.setText(plan.dimension);
        	
    	}
    	else
    	{
    		orderCode = viewHolder.getOrderCode();
    		orderCode.setText("Retail Price : " + plan.price);
    	}
    	
        img=viewHolder.getImage();
        imageDownloader.download(plan.imageURL, (ImageView) img);
         
        return convertView;
    }
    
    private class ViewHolder 
    {
    	private View listRow;
        private TextView title = null;
        private TextView orderCode = null;
        private TextView detail = null;
        private ImageView i11=null; 
        
        private TextView bedbathLabel = null;
        private TextView dimensionLabel = null;
        
        public ViewHolder(View listRow) 
        {
        	this.listRow = listRow;
        }
        
        public TextView getOrderCode() 
        {
        	if(null == orderCode)
        	{
        		orderCode = (TextView) listRow.findViewById(R.id.orderCode);
            }
            return orderCode;
         }     
        
        public TextView getBedBathLabel() 
        {
        	if(null == bedbathLabel)
        	{
        		bedbathLabel = (TextView) listRow.findViewById(R.id.bedbath_label);
            }
            return bedbathLabel;
         }     

        public TextView getDimensionLabel() 
        {
        	if(null == dimensionLabel)
        	{
        		dimensionLabel = (TextView) listRow.findViewById(R.id.dimension_label);
            }
            return dimensionLabel;
         }     
        
        
        public TextView gettitle() 
        {
        	if(null == title)
        	{
        		title = (TextView) listRow.findViewById(R.id.title);
            }
            return title;
         }     
         public TextView getdetail() 
         {
             if(null == detail)
             {
                  detail = (TextView) listRow.findViewById(R.id.detail);
             }
             return detail;
         }
         
         public ImageView getImage() 
         {
        	 if(null == i11){
                  i11 = (ImageView) listRow.findViewById(R.id.img);
        	 }
             return i11;
        }
    }
}
